package ServletMerchandise;

import Dao.McdDao;
import Entity.Merchandise;
import Util.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.awt.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ServletMcdDel", value = "/mcddel")
public class ServletMcdDel extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        int mctid = Integer.parseInt(request.getParameter("mctid"));
        String username = null;

        try {
            username = McdDao.queryusername(mctid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            McdDao.del(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String loc = request.getParameter("loc");
        request.setAttribute("username",username);


        if(loc.equals("update")){
            ArrayList<Merchandise> list = null;
            try {
                list = McdDao.queryBymctid(mctid);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if(list.size()==0){
                request.getRequestDispatcher("merchandise.jsp").forward(request, response);
            }
            else{
                Merchandise merchandise =list.get(0);

                request.setAttribute("merchandise", merchandise);
                request.setAttribute("list",list);

                //获取页码
                int pagenum = 1,total = list.size();
                pagenum = Integer.parseInt(request.getParameter("pagenum"));
                Page page = new Page(pagenum,total);
                request.setAttribute("page",page);
//                System.out.println("get:"+page);

                request.getRequestDispatcher("mcdupdate.jsp").forward(request, response);
            }
        }
        else{
            List<Merchandise> list = null;
            try {
                list = McdDao.queryByKey("",username);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            request.setAttribute("list",list);
            //获取页码
            int pagenum = 1,total = list.size();
            pagenum = Integer.parseInt(request.getParameter("pagenum"));
            Page page = new Page(pagenum,total);
            request.setAttribute("page",page);
//            System.out.println("get:"+page);

            request.getRequestDispatcher("merchandise.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
